package com.mysum.netty.client;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

public class MessageClient {
	static AtomicLong count = new AtomicLong(0);
			
	public static void main(String[] args) throws Exception {
		
		// Parse options.
		String host = "127.0.0.1";
		int port = 8080;
		// Configure the client.
		ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
		// Set up the event pipeline factory.
		bootstrap.setPipelineFactory(new MessageClientPipelineFactory());
		// Start the connection attempt.
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
//		future.await().getChannel();
		Channel channel = future.getChannel();
		//如果5s能连接上
		if (future.awaitUninterruptibly(5000L)) {
				if (channel.isWritable()) {
					while (true) {
						
						ChannelFuture wrFuture = channel.write( "hello kafka0102" + count.incrementAndGet());
						 System.out.println(wrFuture.getClass());
						try {
							Thread.sleep(20);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
		}
		// Wait until the connection is closed or the connection attempt fails.
		// future.getChannel().getCloseFuture().awaitUninterruptibly();
		// Shut down thread pools to exit.
		// bootstrap.releaseExternalResources();
	}
}